package com.health;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import com.health.adapter.TongjiAdapter;
import com.health.db.FDbManager;
import com.health.tablebean.ARCHIVE_BASEINFO;
import com.health.tablebean.BD_ORGANIZE_EMPLOYEE;
import com.health.view.spinner.NiceSpinner;
import com.health.widget.EditTextWithDate;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class TongjiActivity extends NovaActivity implements OnClickListener {
	
	private ListView listView;
	private EditText idEt;
	private EditText addrEt;
	private NiceSpinner createrSp;
	private EditTextWithDate birthydayLEt;
	private EditTextWithDate birthydayHEt;
	private NiceSpinner dateTypeSp;
	private EditTextWithDate dateLEt;
	private EditTextWithDate dateHEt;
	private FDbManager fdbmanger;
	private Button queryBtn;
	private TextView nameTv;
	private TextView sexTv;
	private TextView nationTv;
	private TextView birthdayTv;
	private TextView phoneTv;
	private TextView idTv;
	private TextView createDateTv;
	private TextView updateDateTv;
	private TextView examDataTv;
	private List<BD_ORGANIZE_EMPLOYEE> employeeInfos;
	private TongjiAdapter adapter;
	private Button fanhui;
	private Button save;
	private TextView title;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tongji);
		fdbmanger = NovaApplication.fdbmanger;
		fanhui = (Button) findViewById(R.id.btn_back);
		fanhui.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
				
			}
		});
		save = (Button) findViewById(R.id.btn_up);
		save.setVisibility(View.GONE);
		title = (TextView) findViewById(R.id.title);
		title.setText("综合统计");
		findViews();
		setListeners();
		setData();
	}

	private void setData() {
		setListHead();
		List<ARCHIVE_BASEINFO> infos = fdbmanger.findAll(ARCHIVE_BASEINFO.class);
		adapter = new TongjiAdapter(this, infos);
		listView.setAdapter(adapter);
		String[] strs = {"无","体检日期","建档日期"};
		dateTypeSp.attachDataSource(Arrays.asList(strs));
		employeeInfos = fdbmanger.findAll(BD_ORGANIZE_EMPLOYEE.class);
		if (employeeInfos.size()>0) {
			List<String> employeeNames = new ArrayList<String>();
			employeeNames.add("不限");
			for (BD_ORGANIZE_EMPLOYEE employee : employeeInfos) {
				if (!TextUtils.isEmpty(employee.getNAME())) {
					employeeNames.add(employee.getNAME());
				}
			}
			createrSp.attachDataSource(employeeNames);
			
		}
	}

	@SuppressWarnings("deprecation")
	@SuppressLint("ResourceAsColor")
	private void setListHead() {
		View head = View.inflate(this, R.layout.tongji_list_head, null);
		
		nameTv = (TextView) head.findViewById(R.id.tongji_list_name);
		sexTv = (TextView) head.findViewById(R.id.tongji_list_sex);
		nationTv = (TextView) head.findViewById(R.id.tongji_list_nation);
		birthdayTv = (TextView) head.findViewById(R.id.tongji_list_birthday);
		phoneTv = (TextView) head.findViewById(R.id.tongji_list_phone);
		idTv = (TextView) head.findViewById(R.id.tongji_list_idcard);
		createDateTv = (TextView) head.findViewById(R.id.tongji_list_createdate);
		updateDateTv = (TextView) head.findViewById(R.id.tongji_list_updatadate);
		examDataTv = (TextView) head.findViewById(R.id.tongji_list_examdate);
		
		nameTv.setOnClickListener(this);
		sexTv.setOnClickListener(this);
		nationTv.setOnClickListener(this);
		birthdayTv.setOnClickListener(this);
		phoneTv.setOnClickListener(this);
		idTv.setOnClickListener(this);
		createDateTv.setOnClickListener(this);
		updateDateTv.setOnClickListener(this);
		examDataTv.setOnClickListener(this);
		
		
		listView.addHeaderView(head);
	}

	@Override
	public void findViews() {
		listView = (ListView) findViewById(R.id.tongji_list_lv);
		idEt = (EditText) findViewById(R.id.tongji_idcard_et);
		addrEt = (EditText) findViewById(R.id.tongji_addr_et);
		createrSp = (NiceSpinner) findViewById(R.id.tongji_creater_et);
		birthydayLEt = (EditTextWithDate) findViewById(R.id.tongji_birthday_low);
		birthydayHEt = (EditTextWithDate) findViewById(R.id.tongji_birthday_hight);
		dateTypeSp = (NiceSpinner) findViewById(R.id.tongji_datetype_sp);
		dateLEt = (EditTextWithDate) findViewById(R.id.tongji_date_low);
		dateHEt = (EditTextWithDate) findViewById(R.id.tongji_date_hight);
		queryBtn = (Button)findViewById(R.id.tongji_query_btn);
		
	}

	@Override
	public void setListeners() {
		queryBtn.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.tongji_query_btn) {
			reQueryList();
		} else if (id == R.id.tongji_list_name) {
		} else if (id == R.id.tongji_list_sex) {
		} else if (id == R.id.tongji_list_nation) {
		} else if (id == R.id.tongji_list_birthday) {
		} else if (id == R.id.tongji_list_phone) {
		} else if (id == R.id.tongji_list_idcard) {
		} else if (id == R.id.tongji_list_createdate) {
		} else if (id == R.id.tongji_list_updatadate) {
		} else if (id == R.id.tongji_list_examdate) {
		} else {
		}

	}

	private void reQueryList() {
		String nameOrId = idEt.getText().toString();
		String addr = addrEt.getText().toString();
		BD_ORGANIZE_EMPLOYEE employee = employeeInfos.get(createrSp.getSelectedIndex());
		String creater = "%";//employee.getNAME();
		Date birthdayLDate = birthydayLEt.getDate();
		Date birthdayHDate = birthydayHEt.getDate();
		String dateType = dateTypeSp.getText().toString();
		Date dateL = dateLEt.getDate();
		Date dateH = dateHEt.getDate();
		nameOrId = TextUtils.isEmpty(nameOrId)?"%":nameOrId;
		addr = TextUtils.isEmpty(addr)?"%":addr;
		creater = TextUtils.isEmpty(creater)?"%":creater;
		birthdayLDate = birthdayLDate ==null?new Date(0, 0, 0):birthdayLDate;
		birthdayHDate = birthdayHDate ==null?new Date(2900, 0, 0):birthdayHDate;
		dateL = (dateL ==null?new Date(0, 0, 0):dateL);
		dateH = (dateH ==null?new Date(2900, 0, 0):dateH);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String strWhere = (isNumeric(nameOrId)?"IDCARD LIKE '":"CUSTOMERNAME LIKE '")+nameOrId+"' AND ADDR LIKE '"+addr+"' AND CREATED_BY LIKE '"+creater+"' AND BIRTHDAY>'"+format.format(birthdayLDate)+"' AND BIRTHDAY<'"+format.format(birthdayHDate)+"'" ;
		String where2 = "";

		if (dateType.equals("体检日期") || dateType.equals("建档日期")) {
			if (dateL == null || dateH == null) {
				showTextDialog("请输入完整的时间范围");
				return;
			}
			if (dateType.equals("体检日期")) {
				where2 = "AND  LAST_UPDATE_DATE between '" + format.format(dateL)
						+ "'   and '" + format.format(dateH)+"'" ;
			} else if (dateType.equals("建档日期")) {
				where2 = "AND  CREATED_DATE between '" + format.format(dateL)
						+ "'  and '" + format.format(dateH) + "'";

			}
		}
		strWhere = strWhere+where2;
		System.out.println("====================="+where2);
		List<ARCHIVE_BASEINFO> list = fdbmanger.findAllByWhere(ARCHIVE_BASEINFO.class, strWhere);
		adapter.setInfoList(list);
		adapter.notifyDataSetChanged();
		
		
		
	}
	
	
	public static boolean isNumeric(String str){
	    Pattern pattern = Pattern.compile("[0-9]*"); 
	    return pattern.matcher(str).matches();    
	 }

}
